import { formatDateTime } from './utils'

export default function (sqlite) {
  return {
    // 插入分红
    insertDividend(dividendName, isEnable, ratioList) {
      return new Promise((resolve, reject) => {
        const sql = 'INSERT INTO dividend (dividend_name,is_enable,create_time) VALUES (?, ?, ?)'
        sqlite.run(sql, [dividendName, isEnable, formatDateTime()], function (err) {
          if (err) {
            console.error('Error inserting dividend:', err.message)
            reject(err.message)
          } else {
            // resolve(this.lastID)
            const lastID = this.lastID
            ratioList.forEach((item) => {
              const sql =
                'INSERT INTO dividend_ratio (dividend_id,start_money,end_money,ratio,create_time) VALUES (?,?,?,?,?)'
              sqlite.run(
                sql,
                [lastID, item.startMoney, item.endMoney, item.ration, formatDateTime()],
                function (err) {
                  if (err) {
                    console.error('Error inserting dividend_ratio:', err.message)
                    reject(err.message)
                  } else {
                    resolve(lastID)
                  }
                }
              )
            })
          }
        })
      })
    },

    // 获取所有分红规则
    getDividendAll() {
      return new Promise((resolve, reject) => {
        const dataSql = `SELECT * FROM dividend ORDER BY create_time DESC `
        sqlite.all(dataSql, [], (err, rows) => {
          if (err) {
            console.error('Error getDividendAll:', err.message)
            reject(err.message)
          } else {
            resolve(rows)
          }
        })
      })
    },

    getDividendByName(dividendId, dividendName) {
      return new Promise((resolve, reject) => {
        if (dividendId > 0) {
          let dataSql = `SELECT * FROM dividend WHERE dividend_name=? AND dividend_id <> ? `
          sqlite.all(dataSql, [dividendName, dividendId], (err, rows) => {
            if (err) {
              console.error('Error getDividendAll:', err.message)
              reject(err.message)
            } else {
              resolve(rows)
            }
          })
        } else {
          let dataSql = `SELECT * FROM dividend WHERE dividend_name=? `
          sqlite.all(dataSql, [dividendName], (err, rows) => {
            if (err) {
              console.error('Error getDividendAll:', err.message)
              reject(err.message)
            } else {
              resolve(rows)
            }
          })
        }
      })
    },

    //获取有效的分红规则
    getDividendByIsEnable(isEnable) {
      return new Promise((resolve, reject) => {
        let dataSql = `SELECT * FROM dividend WHERE is_enable=? `
        sqlite.all(dataSql, [isEnable], (err, rows) => {
          if (err) {
            console.error('Error getDividendAll:', err.message)
            reject(err.message)
          } else {
            resolve(rows)
          }
        })
      })
    },

    // 获取所有分红规则详情
    getDividendDetail(dividendId) {
      return new Promise((resolve, reject) => {
        const dataSql = `SELECT * FROM dividend_ratio WHERE dividend_id = ? ORDER BY create_time ASC `
        sqlite.all(dataSql, [dividendId], (err, rows) => {
          if (err) {
            console.error('Error getDividendDetail:', err.message)
            reject(err.message)
          } else {
            // console.log('getDividendDetail', rows)

            resolve(rows)
          }
        })
      })
    },

    // 修改分红规则
    updateDividend(
      dividendId,
      dividendName,
      isEnable,
      addRatioList,
      updateRatioList,
      delRatioList
    ) {
      return new Promise((resolve, reject) => {
        const sql = 'UPDATE dividend SET dividend_name=?,is_enable=? WHERE dividend_id=?'
        sqlite.run(sql, [dividendName, isEnable, dividendId], (err, rows) => {
          if (err) {
            console.error('Error update dividend:', err.message)
            reject(err.message)
          } else {
            // resolve(this.changes > 0)
            //修改
            updateRatioList.forEach((item) => {
              const sql =
                'UPDATE dividend_ratio SET start_money=?,end_money=?,ratio=? WHERE dividend_ratio_id=?'
              sqlite.run(
                sql,
                [item.startMoney, item.endMoney, item.ration, item.dividendRatioId],
                (err, rows) => {
                  if (err) {
                    console.error('Error update dividend_ratio:', err.message)
                    reject(err.message)
                  } else {
                    resolve(this.changes > 0)
                  }
                }
              )
            })
            //新增
            addRatioList.forEach((item) => {
              const sql =
                'INSERT INTO dividend_ratio (dividend_id,start_money,end_money,ratio,create_time) VALUES (?,?,?,?,?)'
              sqlite.run(
                sql,
                [dividendId, item.startMoney, item.endMoney, item.ration, formatDateTime()],
                function (err) {
                  if (err) {
                    console.error('Error inserting dividend_ratio:', err.message)
                  }
                }
              )
            })
            //删除
            delRatioList.forEach((item) => {
              const sql = ' DELETE FROM dividend_ratio WHERE dividend_ratio_id=? '
              sqlite.run(sql, [item.dividendRatioId], (err) => {
                if (err) {
                  console.error('Error delete dividend_ratio:', err.message)
                }
              })
            })
          }
        })
      })
    }
  }
}
